home *** CD-ROM | disk | FTP | other *** search
- #include "SFConvert.h"
- #include <stdio.h>
- #include <unix.h>
- #include <string.h>
- #include <math.h>
- #include <SANE.h>
- #include "SDtype.h"
- #include "AIFFType.h"
-
-
-
- extern OSErr theErr;
- extern int FileOpen;
- extern long TotalSamps;
- extern CursHandle watchCurs;
- extern int SoundFileType;
- extern long SampleRate;
- extern long NumChannels;
- extern double MaxSample;
- extern double MinSample;
- extern int nrec;
- extern long fileSize;
- extern ioParam myIOParmBlk;
- extern ioParam NewParmBlk;
- extern int SFOUTPUTtype;
- extern Boolean SDnoResource;
- extern long RecLength;
- extern Str255 NewSoundFileName;
-
- Boolean FloatToINT16(void);
- Boolean FloatToCHUNKY(void);
- Boolean FloatToSD1(void);
- Boolean FloatToSD2(void);
- Boolean FloatToAIFF(void);
-
- extern void PstringCopy(char *, char *);
- extern void PstringCat(char *, char *);
- extern void DoOSErrorAlert(Str255, Str255);
-
-
- Boolean FloatToINT16()
- {
- register long i;
- register double offset;
- register double scalefactor;
- register double x;
- Str255 mess;
- int *theIbuf, *Iptr;
- float *sp, *SampBuf;
- long nBytes;
- long nSamps;
- long bytesLeft;
- long sampBufsz;
-
-
- RecLength = (long)(16384);
-
- nrec = 0;
- SetProgressDialog();
-
- TotalSamps = fileSize / sizeof(float);
- myIOParmBlk.ioReqCount = TotalSamps * sizeof(int);
- myIOParmBlk.ioCompletion = NIL;
- if ( (theErr = PBAllocContig(&myIOParmBlk, FALSE)) != noErr ) {
- if ( theErr == dskFulErr ) {
- if ( (theErr = PBAllocate(&myIOParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pCan't allocate diskspace for contig file", NIL);
- }
- }
- }
-
- sampBufsz = RecLength / sizeof(float);
- theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
- SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
-
-
- /* offset = fabs(MinSample);
- scalefactor = SAMPMAX / (offset + MaxSample);
- */
- scalefactor = fabs(MinSample);
- if ( fabs(MaxSample) > fabs(MinSample))
- scalefactor = (SAMPMAX / 2.0) / fabs(MaxSample);
- else
- scalefactor = (SAMPMAX / 2.0) / fabs(MinSample);
-
-
- bytesLeft = fileSize;
-
- myIOParmBlk.ioPosMode = fsAtMark;
- myIOParmBlk.ioPosOffset = NIL;
-
- NewParmBlk.ioPosMode = fsAtMark;
- NewParmBlk.ioPosOffset = NIL;
-
-
- while ( bytesLeft > 0L ) {
- if ( bytesLeft > RecLength )
- nBytes = RecLength;
- else
- nBytes = bytesLeft;
-
- myIOParmBlk.ioBuffer = (Ptr)SampBuf;
- myIOParmBlk.ioReqCount = nBytes;
- theErr = PBRead(&myIOParmBlk, FALSE);
- if (theErr != noErr ) {
- if ( theErr == eofErr )
- DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
- }
- nBytes = myIOParmBlk.ioActCount;
- nSamps = nBytes / sizeof(float);
- for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
- x = *sp++ * scalefactor;
- *Iptr++ = (int)x;
- }
-
- /* write it out */
- NewParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
- NewParmBlk.ioBuffer = (Ptr)theIbuf;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(int)) ) {
- DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
- }
-
- nrec++;
- if ( !UpdateProgressDialog() ) {
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(FALSE);
- }
- bytesLeft -= nBytes;
- }
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(TRUE);
- }
-
-
- Boolean FloatToCHUNKY()
- {
- register long i;
- register double offset;
- register double scalefactor;
- register double x;
- Str255 mess;
- unsigned int *theIbuf, *Iptr;
- float *sp, *SampBuf;
- long nBytes;
- long nSamps;
- long bytesLeft;
- long sampBufsz;
- unsigned char *highBuf, *h, *lowBuf, *l;
- unsigned char *RhighBuf, *Rh, *RlowBuf, *Rl;
-
-
-
- RecLength = (long)(32768);
-
- nrec = 0;
- SetProgressDialog();
-
- TotalSamps = fileSize / sizeof(float);
-
- sampBufsz = RecLength / sizeof(float);
- theIbuf = (unsigned int *)NewPtr(sizeof(int) * sampBufsz);
- if ( (theErr = MemError()) != noErr ) {
- DoOSErrorAlert("\pcan't get enough memory for theIbuf", NIL);
- return(FALSE);
- }
- SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
- if ( (theErr = MemError()) != noErr ) {
- DoOSErrorAlert("\pcan't get enough memory for SampBuf", NIL);
- return(FALSE);
- }
- highBuf = (unsigned char *)NewPtr(sampBufsz);
- if ( (theErr = MemError()) != noErr ) {
- DoOSErrorAlert("\pcan't get enough memory for highBuf", NIL);
- return(FALSE);
- }
- lowBuf = (unsigned char *)NewPtr(sampBufsz);
- if ( (theErr = MemError()) != noErr ) {
- DoOSErrorAlert("\pcan't get enough memory for lowBuf", NIL);
- return(FALSE);
- }
- if ( NumChannels == 2 ) {
- RhighBuf = (unsigned char *)NewPtr(sampBufsz);
- if ( (theErr = MemError()) != noErr ) {
- DoOSErrorAlert("\pcan't get enough memory for RhighBuf", NIL);
- return(FALSE);
- }
- RlowBuf = (unsigned char *)NewPtr(sampBufsz);
- if ( (theErr = MemError()) != noErr ) {
- DoOSErrorAlert("\pcan't get enough memory for RlowBuf", NIL);
- return(FALSE);
- }
- }
-
- offset = fabs(MinSample);
- scalefactor = SAMPMAX / (offset + MaxSample);
-
-
- bytesLeft = fileSize;
-
- myIOParmBlk.ioPosMode = fsAtMark;
- myIOParmBlk.ioPosOffset = NIL;
-
- NewParmBlk.ioPosMode = fsAtMark;
- NewParmBlk.ioPosOffset = NIL;
-
-
- while ( bytesLeft > 0L ) {
- if ( bytesLeft > RecLength )
- nBytes = RecLength;
- else
- nBytes = bytesLeft;
-
- myIOParmBlk.ioBuffer = (Ptr)SampBuf;
- myIOParmBlk.ioReqCount = nBytes;
- theErr = PBRead(&myIOParmBlk, FALSE);
- if (theErr != noErr ) {
- if ( theErr == eofErr )
- DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
- }
- nBytes = myIOParmBlk.ioActCount;
- nSamps = nBytes / sizeof(float);
- for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
- x = ((*sp++ + offset ) * scalefactor);
- *Iptr++ = (unsigned int)x;
- }
- /* convert to CHUNKY format */
- Iptr = theIbuf;
- h = highBuf;
- l = lowBuf;
- if ( NumChannels == 2 ) {
- Rh = RhighBuf;
- Rl = RlowBuf;
- }
- for ( i = 0; i < nSamps; i++ ) {
- *h++ = (unsigned char)((*Iptr & 0xFF00) >> 8);
- *l++ = (unsigned char )(*Iptr & 0x00FF);
- Iptr++;
- if ( NumChannels == 2) {
- *Rh++ = (unsigned char)(*Iptr & 0xFF00) >> 8;
- *Rl++ = (unsigned char)*Iptr & 0x00FF;
- Iptr++;
- }
- }
-
- /* write it out */
- NewParmBlk.ioReqCount = (long)nSamps;
- NewParmBlk.ioBuffer = (Ptr)highBuf;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != (long)(nSamps) ) {
- DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
- }
-
- NewParmBlk.ioBuffer = (Ptr)lowBuf;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != (long)(nSamps) ) {
- DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
- }
-
- if ( NumChannels == 2 ) {
- NewParmBlk.ioReqCount = (long)nSamps;
- NewParmBlk.ioBuffer = (Ptr)RhighBuf;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != (long)(nSamps) ) {
- DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
- }
-
- NewParmBlk.ioBuffer = (Ptr)RlowBuf;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != (long)(nSamps) ) {
- DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
- }
- }
- nrec++;
- if ( !UpdateProgressDialog() ) {
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(FALSE);
- }
- bytesLeft -= nBytes;
- }
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(TRUE);
- }
-
- Boolean FloatToSD1()
- {
- register long i;
- register double scalefactor;
- register double x;
- double peak;
- Str255 mess;
- int *theIbuf, *Iptr;
- float *sp, *SampBuf;
- long nBytes;
- long nSamps;
- long bytesLeft;
- long sampBufsz;
- WaveRec Header;
- WavPtr HeaderPtr;
-
-
- RecLength = (long)(16384);
- HeaderPtr = &Header;
-
- TotalSamps = fileSize / sizeof(float);
-
-
-
-
- /* initialize Header info to default values */
- Header.HeaderSize = 1336;
- Header.Version = 0;
- Header.Preview = 0;
- Header.WPtr = 0L;
- Header.WPeek = 0L;
- Header.HInxPage = 0L;
- Header.HInxLine = 0L;
- Header.VInxPage = 0L;
- Header.VInxLine = 0L;
- Header.HCtlPage = 0L;
- Header.HCtlLine = 0L;
- Header.VCtlPage = 255L;
- Header.VCtlLine = 65536L;
- Header.VOffset = 0L;
- Header.HOffset = 0L;
- Header.VOffConst = -1L;
- Header.ZF.v = -256;
- Header.ZF.h = -128L;
- Header.SF.VFactor = 327L;
- Header.SF.VType = 1032;
- PstringCopy(&Header.SF.VString, "\p%Scale");
- Header.SF.HFactor = 1;
- Header.SF.HType = 30L;
- PstringCopy(&Header.SF.HString, "\p secs");
- Header.VScrUpdate = 0;
- Header.BufPtr = NIL;
- Header.BufBytes = 0L;
- Header.BufOffset = 0L;
- Header.WaveRgn = 0L;
- /* (**Header.WaveRgn).rgnSize = 0L;
- (**Header.WaveRgn).rgnBBox.top = 0;
- (**Header.WaveRgn).rgnBBox.left = 16512;
- (**Header.WaveRgn).rgnBBox.bottom = 10772;
- (**Header.WaveRgn).rgnBBox.right = 58;
- */
- Header.ClipArea = 0L;
- Header.ScaleArea = 0L;
- Header.CtlWidth.top = 0;
- Header.CtlWidth.left = 29;
- Header.CtlWidth.bottom = 33;
- Header.CtlWidth.right = 15;
- Header.VScroll = NIL;
- Header.HScroll = NIL;
-
- Header.FileSize = (long)(TotalSamps * sizeof(int));
- PstringCopy(&Header.BUName, "\p");
- PstringCopy(&Header.FileName, NewSoundFileName);
- Header.BURefNum = 0;
- Header.refNum = NewParmBlk.ioRefNum;
- Header.vRefNum = NewParmBlk.ioVRefNum;
- Header.BufChanged = 0;
- Header.FileChanged = 0;
- Header.NoBackup = 0;
- Header.Mode = 0;
- Header.Edit.HiAddr = 0L;
- Header.Edit.LoAddr = 0L;
- Header.Edit.ExtendSide = 0;
- Header.CursorPos = 0L;
- Header.CursorRgn = 0L;
- for ( i = 0; i < 10; i++ ) {
- Header.MarkerData[i].Free = 1;
- Header.MarkerData[i].Position = 0L;
- PstringCopy(&Header.MarkerData[i].Name, "\p");
- }
- Header.MarkerOffset = 0L;
- Header.LoopStart = -1L;
- Header.LoopEnd = -1L;
- Header.ZeroLineOn = 0;
- Header.CursorOn = 0;
- Header.ScalesOn = 511;
- PstringCopy(&Header.Comment, "\p");
- Header.SampRate = SampleRate;
- x = (1.0/(double)SampleRate) * 1000000.0;
- Header.SampPeriod = (long)(x + 0.5);
- Header.SampSize = 16;
- PstringCopy(&Header.CodeType, "\pLinear");
- PstringCopy(&Header.UserStr1, "\p");
- Header.BufSize = 0L;
- Header.Loop2Start = -1L;
- Header.Loop2End = -1L;
- Header.Loop1Type = 1;
- Header.Loop2Type = 1;
- Header.User4 = 0;
-
- /* write out header infor */
- NewParmBlk.ioReqCount = Header.HeaderSize;
- NewParmBlk.ioBuffer = (Ptr)HeaderPtr;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing Header to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != Header.HeaderSize) {
- DoOSErrorAlert("\pError writing Header to file, wrote wrong number of bytes", NIL);
- }
-
-
-
-
- nrec = 0;
- SetProgressDialog();
-
-
- sampBufsz = RecLength / sizeof(float);
- theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
- SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
-
-
- /* offset = fabs(MinSample);
- scalefactor = SAMPMAX / (offset + MaxSample);
- */
-
- peak = MAX(fabs(MinSample), fabs(MaxSample));
- scalefactor = (SAMPMAX / 2.0) / peak;
- bytesLeft = fileSize;
-
- myIOParmBlk.ioPosMode = fsAtMark;
- myIOParmBlk.ioPosOffset = NIL;
-
- NewParmBlk.ioPosMode = fsAtMark;
- NewParmBlk.ioPosOffset = NIL;
-
-
- while ( bytesLeft > 0L ) {
- if ( bytesLeft > RecLength )
- nBytes = RecLength;
- else
- nBytes = bytesLeft;
-
- myIOParmBlk.ioBuffer = (Ptr)SampBuf;
- myIOParmBlk.ioReqCount = nBytes;
- theErr = PBRead(&myIOParmBlk, FALSE);
- if (theErr != noErr ) {
- if ( theErr == eofErr )
- DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
- }
- nBytes = myIOParmBlk.ioActCount;
- nSamps = nBytes / sizeof(float);
- for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
- /* x = ((*sp++ + offset ) * scalefactor);*/
- x = *sp++ * scalefactor;
- if ( x < MinSample )
- MinSample = x;
- if ( x > MaxSample )
- MaxSample = x;
- *Iptr++ = (int)x;
- }
-
- /* write it out */
- NewParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
- NewParmBlk.ioBuffer = (Ptr)theIbuf;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(int)) ) {
- DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
- }
-
- nrec++;
- if ( !UpdateProgressDialog() ) {
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(FALSE);
- }
- bytesLeft -= nBytes;
- }
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(TRUE);
- }
-
- Boolean FloatToSD2()
- {
- register long i;
- register double scalefactor;
- register double x;
- double peak;
- Str255 mess;
- int *theIbuf, *Iptr;
- float *sp, *SampBuf;
- long nBytes;
- long nSamps;
- long bytesLeft;
- long sampBufsz;
-
-
- RecLength = (long)(16384);
-
-
- /* SD 2 format - just like SD1 soundfile data part, do resources later */
-
- nrec = 0;
- SetProgressDialog();
-
- sampBufsz = RecLength / sizeof(float);
- theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
- SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
-
-
- peak = MAX(fabs(MinSample), fabs(MaxSample));
- scalefactor = (SAMPMAX / 2.0) / peak;
- bytesLeft = fileSize;
-
- myIOParmBlk.ioPosMode = fsAtMark;
- myIOParmBlk.ioPosOffset = NIL;
-
- NewParmBlk.ioPosMode = fsAtMark;
- NewParmBlk.ioPosOffset = NIL;
-
-
- while ( bytesLeft > 0L ) {
- if ( bytesLeft > RecLength )
- nBytes = RecLength;
- else
- nBytes = bytesLeft;
-
- myIOParmBlk.ioBuffer = (Ptr)SampBuf;
- myIOParmBlk.ioReqCount = nBytes;
- theErr = PBRead(&myIOParmBlk, FALSE);
- if (theErr != noErr ) {
- if ( theErr == eofErr )
- DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
- }
- nBytes = myIOParmBlk.ioActCount;
- nSamps = nBytes / sizeof(float);
- for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
- /* x = ((*sp++ + offset ) * scalefactor);*/
- x = *sp++ * scalefactor;
- if ( x < MinSample )
- MinSample = x;
- if ( x > MaxSample )
- MaxSample = x;
- *Iptr++ = (int)x;
- }
-
- /* write it out */
- NewParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
- NewParmBlk.ioBuffer = (Ptr)theIbuf;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(int)) ) {
- DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
- }
-
- nrec++;
- if ( !UpdateProgressDialog() ) {
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(FALSE);
- }
- bytesLeft -= nBytes;
- }
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(TRUE);
- }
-
- Boolean FloatToAIFF()
- {
- register long i;
- register double scalefactor;
- register double x;
- double peak;
- int *theIbuf, *Iptr;
- float *sp, *SampBuf;
- long nBytes;
- long nSamps;
- long bytesLeft;
- long sampBufsz;
-
-
- Chunk FormChunkHeader, *FormChunkHeaderPtr;
- CommonChunk CommonHeader, *CommonHeaderPtr;
- SoundDataChunk SoundDataHeader, *SoundDataHeaderPtr;
- MarkerChunk MarkerChunkHeader, *MarkerChunkHeaderPtr;
- InstrumentChunk InstHeader, *InstHeaderPtr;
- Boolean oddByte;
- int theInt;
- int theChar;
- double aDouble;
- int kk;
- Str255 myStr255;
-
-
- RecLength = (long)(16384);
- FormChunkHeaderPtr = &FormChunkHeader;
- CommonHeaderPtr = &CommonHeader;
- SoundDataHeaderPtr = &SoundDataHeader;
- TotalSamps = fileSize / sizeof(float);
- /* initialize Form Chunk info */
- FormChunkHeader.ckID = 'FORM';
- FormChunkHeader.formType = 'AIFF';
-
-
- /* initialize Common Chunk info */
- CommonHeader.ckID = 'COMM';
- CommonHeader.ckSize = sizeof( CommonHeader.numChannels ) +
- sizeof( CommonHeader.numSampleFrames ) +
- sizeof( CommonHeader.sampleSize ) +
- sizeof( CommonHeader.sampleRate );
- CommonHeader.numChannels = (short)NumChannels;
- CommonHeader.numSampleFrames = (unsigned long)(TotalSamps / NumChannels);
- CommonHeader.sampleSize = SixteenBits;
-
- NumToString(SampleRate, &myStr255);
- CommonHeader.sampleRate = str2num(&myStr255); /* string to extended */
-
-
-
-
- SoundDataHeader.ckID = 'SSND';
- SoundDataHeader.offset = 0L;
- SoundDataHeader.blockSize = 0L;
-
- SoundDataHeader.ckSize = sizeof( SoundDataHeader.offset ) +
- sizeof( SoundDataHeader.blockSize ) +
- (long)(TotalSamps * sizeof(int));
-
-
-
- FormChunkHeader.ckSize = sizeof(FormChunkHeader.formType);
-
- FormChunkHeader.ckSize += (sizeof(CommonHeader.ckID) +
- sizeof(CommonHeader.ckSize)
- + CommonHeader.ckSize);
-
- FormChunkHeader.ckSize += (sizeof(SoundDataHeader.ckID) +
- sizeof(SoundDataHeader.ckSize)
- + SoundDataHeader.ckSize);
-
- if ( FormChunkHeader.ckSize % 2 != 0 )
- oddByte = TRUE;
- else
- oddByte = FALSE;
-
-
- /* write out header info */
- NewParmBlk.ioReqCount = sizeof(FormChunkHeader);
- NewParmBlk.ioBuffer = (Ptr)FormChunkHeaderPtr;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing Header to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != NewParmBlk.ioReqCount) {
- DoOSErrorAlert("\pError writing Header to file, wrote wrong number of bytes", NIL);
- }
-
- NewParmBlk.ioReqCount = sizeof(CommonHeader);
- NewParmBlk.ioBuffer = (Ptr)CommonHeaderPtr;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing Header to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != NewParmBlk.ioReqCount) {
- DoOSErrorAlert("\pError writing Header to file, wrote wrong number of bytes", NIL);
- }
-
- NewParmBlk.ioReqCount = sizeof(SoundDataHeader);
- NewParmBlk.ioBuffer = (Ptr)SoundDataHeaderPtr;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing Header to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != NewParmBlk.ioReqCount) {
- DoOSErrorAlert("\pError writing Header to file, wrote wrong number of bytes", NIL);
- }
-
- nrec = 0;
- SetProgressDialog();
-
-
- sampBufsz = RecLength / sizeof(float);
- theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
- SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
-
-
- /* offset = fabs(MinSample);
- scalefactor = SAMPMAX / (offset + MaxSample);
- */
-
- peak = MAX(fabs(MinSample), fabs(MaxSample));
- scalefactor = (SAMPMAX / 2.0) / peak;
- bytesLeft = fileSize;
-
- myIOParmBlk.ioPosMode = fsAtMark;
- myIOParmBlk.ioPosOffset = NIL;
-
- NewParmBlk.ioPosMode = fsAtMark;
- NewParmBlk.ioPosOffset = NIL;
-
-
- while ( bytesLeft > 0L ) {
- if ( bytesLeft > RecLength )
- nBytes = RecLength;
- else
- nBytes = bytesLeft;
-
- myIOParmBlk.ioBuffer = (Ptr)SampBuf;
- myIOParmBlk.ioReqCount = nBytes;
- theErr = PBRead(&myIOParmBlk, FALSE);
- if (theErr != noErr ) {
- if ( theErr == eofErr )
- DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
- }
- nBytes = myIOParmBlk.ioActCount;
- nSamps = nBytes / sizeof(float);
- for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
- /* x = ((*sp++ + offset ) * scalefactor);*/
- /* x = *sp++ * scalefactor;*/
- x = *sp++;
- x *= scalefactor;
- if ( x < MinSample )
- MinSample = x;
- if ( x > MaxSample )
- MaxSample = x;
- kk = (int)x;
- *Iptr++ = (int)x;
- }
-
- /* write it out */
- NewParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
- NewParmBlk.ioBuffer = (Ptr)theIbuf;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing to sample file", NIL);
- }
- if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(int)) ) {
- DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
- }
-
- nrec++;
- if ( !UpdateProgressDialog() ) {
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(FALSE);
- }
- bytesLeft -= nBytes;
- }
-
- if ( oddByte ) {
- NewParmBlk.ioReqCount = 1L;
- theChar = '\0';
- NewParmBlk.ioBuffer = (Ptr)theChar;
- if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
- DoOSErrorAlert("\pError writing to sample file", NIL);
- }
- }
-
-
-
-
-
- DisposPtr((Ptr)SampBuf);
- DisposPtr((Ptr)theIbuf);
- DisposeProgDialog();
- InitCursor();
- return(TRUE);
- }